/*
 * GraphNodeInfoPanel.java
 *
 * Created on June 26, 2008, 10:25 PM
 */
package pl.edu.agh.ssm.monitor.gui;

import java.util.LinkedList;
import java.util.List;
import javax.swing.Icon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
import pl.edu.agh.ssm.monitor.SessionMonitorApp;
import pl.edu.agh.ssm.monitor.data.SessionNode;
import pl.edu.agh.ssm.monitor.utils.IconUtil;

/**
 *
 * @author  aneezka
 */
@SuppressWarnings("serial")
public class GraphNodeInfoPanel extends javax.swing.JPanel implements UpdatePanel, IconPanel {

    /** Creates new form GraphNodeInfoPanel */
    public GraphNodeInfoPanel(SessionNode node) {
        this.node = node;
        initComponents();
        if (node.getNodeDescription() != null) {
            JPanel panel = new SourceDescriptionPanel(node);
            final JScrollPane pane = new JScrollPane(panel);
            pane.setBorder(new TitledBorder(panel.getName()));
            jPanel2.add(pane);
            panels.add((UpdatePanel) panel);
            ((UpdatePanel) panel).addListener(new UpdatePanelListener() {

                public void panelUpdated(UpdatePanel panel) {
                }

                public void panelRemoved(UpdatePanel panel) {
                    jPanel2.remove(pane);
                    jPanel2.revalidate();
                    panels.remove(panel);
                }
            });
        }
        if (node.getSenderInfo() != null) {
            addSRPanel(new SenderReportPanel(node));
        }
    }

    public void update() {
        jLabel1.setText("" + node.getNodeType() + " " + node.getAddress().getHostAddress() + " (SSRC: " + node.getSsrc() + ")");
        if (node.getSenderInfo() != null && srPanel == null) {
            addSRPanel(new SenderReportPanel(node));
        }
        for (UpdatePanel p : panels) {
            p.update();
        }
        for (UpdatePanelListener l : listeners) {
            l.panelUpdated(this);
        }
    }

    public void remove() {
        for (UpdatePanelListener l : listeners) {
            l.panelRemoved(this);
        }
    }

    public void addListener(UpdatePanelListener listener) {
        listeners.add(listener);
    }

    public void removeListener(UpdatePanelListener listener) {
        listeners.remove(listener);
    }

    public Icon getIcon() {
        return (new IconUtil()).getNodeTypeIcon(node);
    }

    private void addSRPanel(SenderReportPanel panel) {
        srPanel = panel;
        final JScrollPane pane = new JScrollPane(panel);
        pane.setBorder(new TitledBorder(panel.getName()));
        jPanel2.add(pane);
        panels.add((UpdatePanel) panel);
        ((UpdatePanel) panel).addListener(new UpdatePanelListener() {

            public void panelUpdated(UpdatePanel panel) {
            }

            public void panelRemoved(UpdatePanel panel) {
                jPanel2.remove(pane);
                jPanel2.revalidate();
                panels.remove(panel);
                srPanel = null;
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();

        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pl.edu.agh.ssm.monitor.SessionMonitorApp.class).getContext().getResourceMap(GraphNodeInfoPanel.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText("" + node.getNodeType() + " " + node.getAddress().getHostAddress() + " (SSRC: " + node.getSsrc() + ")" );
        jLabel1.setName("jLabel1"); // NOI18N

        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setToolTipText(resourceMap.getString("jButton1.toolTipText")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 281, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        SessionMonitorApp.getApplication().getView().updateBottomTab(this);
    }//GEN-LAST:event_jButton1ActionPerformed
    private SessionNode node;
    private List<UpdatePanel> panels = new LinkedList<UpdatePanel>();
    private List<UpdatePanelListener> listeners = new LinkedList<UpdatePanelListener>();
    private SenderReportPanel srPanel = null;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}
